#region File Description
//-----------------------------------------------------------------------------
// Hud.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using RolePlayingGameData;
using DC2010;
using DC2010.Objects;
using DC2010.Objects.Base;
using RolePlaying.Events;
using log4net;
using System.Reflection;
using System.Timers;
using Xna.BaseObjects;
using Xna.Base;
using DC2010.Objects.Data;
using DC2012.Screens;
using RolePlaying;
using Microsoft.Xna.Framework.Input;
#endregion

namespace DC2010
{
    /// <summary>
    /// Displays each player's basic statistics and the combat action menu.
    /// </summary>
    public partial class Hud
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private ScreenManager screenManager;
        private AnimatingSprite an_cursor = new AnimatingSprite();
        private Texture2D cursorTexture;

        //timer which will reset movementpad icons back to default view
        private Timer MovementPadTimer { get; set; }
        private string DefaultMovePadTexture { get; set; }

        #region Initialization


        /// <summary>
        /// Creates a new Hud object using the given ScreenManager.
        /// </summary>
        public Hud(ScreenManager screenManager)
        {
            // check the parameter
            if (screenManager == null)
            {
                throw new ArgumentNullException("screenManager");
            }

            this.screenManager = screenManager;

            lm[0] = new LogMessage("", Color.White);
            lm[1] = new LogMessage("", Color.White);
            lm[2] = new LogMessage("", Color.White);
            lm[3] = new LogMessage("", Color.White);

//            Root.I.GameSystem.Log_Common += new DC2010.Systems.GameSystem.LogDelegate(GameSystem_Log_Common);
  //          Root.I.GameSystem.Log_Debug += new DC2010.Systems.GameSystem.LogDelegate(GameSystem_Log_Common);

            EventsInventory ei = new EventsInventory();
            EventsActionPanel eap = new EventsActionPanel();
            EventsPlayerPanel epp = new EventsPlayerPanel();
            EventsSpellSheet ess = new EventsSpellSheet();
            EventsDungeon ed = new EventsDungeon();

            Timer LogCleanerTimer = new Timer(3000);
            LogCleanerTimer.Elapsed += new ElapsedEventHandler(LogCleanerTimer_Elapsed);
            LogCleanerTimer.Enabled = true;

            //initialize movement timer.
            MovementPadTimer = new Timer();
            MovementPadTimer.Enabled = true;
            MovementPadTimer.Elapsed += new ElapsedEventHandler(MovementPadTimer_Elapsed);
            MovementPadTimer.Interval = 500;
            Tools.MovePadTexture = "Interface/WalkingPad";
            DefaultMovePadTexture = "Interface/WalkingPad";
        }

        void MovementPadTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Tools.MovePadTexture = DefaultMovePadTexture;
        }

        void LogCleanerTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
           // Root.I.GameSystem.LogCommon("1234567890 - ABCDEFGHIJKLMNOPQRSTUVWXYZ", Color.White);
        }

        //List<LogMessage> lm = new List<LogMessage>();
        LogMessage[] lm = new LogMessage[11];
        void GameSystem_Log_Common(string p, Color color)
        {
            //cycle
            lm[3] = lm[2];
            lm[2] = lm[1];
            lm[1] = lm[0];

            lm[0] = new LogMessage(p, color);
        }


        /// <summary>
        /// Load the graphics content from the content manager.
        /// </summary>
        public void LoadContent()
        {
            //wrong place...
            ContentManager content = screenManager.Game.Content;
            //AS.I.AddAnimation("Cursor");
        }


        #endregion


        public void Update(GameTime gameTime)
        {
            TM.Update(gameTime);
        }


        /// <summary>
        /// Draw the screen.
        /// </summary>
        public void Draw(GameTime gameTime)
        {
            if (Root.I.GameSystem != null)
            {
                SpriteBatch spriteBatch = screenManager.SpriteBatch;

                spriteBatch.Begin();

                //DrawForNonCombat();

                DrawActionPanels();
                DrawSpellSheet();
                DrawInventory();

                DrawTreasure();
                DrawScroll();
                DrawStats();
                DrawWalkingPad();

                DrawActivatedActionPanels();

                DrawLog(new Vector2(5, 358));
                DrawPlayerPanels();
                DrawCursor(gameTime);

                DrawCoordinates();

                DrawRectangles();



                spriteBatch.End();
            }
        }

        private void DrawRectangles()
        {

            List<string> rects = RM.IsPointInRectangle(Root.I.GameSystem.CurrentScreen, InputManager.CurrentMouseState.X + 16, InputManager.CurrentMouseState.Y + 16);
            SpriteBatch spriteBatch = screenManager.SpriteBatch;

            var t = new Texture2D(screenManager.GraphicsDevice, 1, 1);
            t.SetData(new[] { Color.White });

            foreach (string s in rects)
            {
                ActiveRectangle ar = RM.Get(s);

                if (ar != null)
                {
                    Rectangle r = ar.rect;
                    int bw = 1; // Border width

                    spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, bw, r.Height), Color.White); // Left
                    spriteBatch.Draw(t, new Rectangle(r.Right, r.Top, bw, r.Height), Color.White); // Right
                    spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, r.Width, bw), Color.White); // Top
                    spriteBatch.Draw(t, new Rectangle(r.Left, r.Bottom, r.Width, bw), Color.White); // Bottom

                    spriteBatch.DrawString(Fonts.DebugFont, ar.Name.ToUpper()+
                        "\nX:"+r.Left+" - "+r.Right+
                        "\nY" + r.Top + " - " + r.Bottom
                    , new Vector2(r.Left+ r.Width + 5, r.Top + 5), Color.White);

                }
            }

            //Rectangle r = new Rectangle(0,0,100,100);
            ////spriteBatch.Draw(t, r, Color.arg,);
            //r.Inflate(1,1);

            //int bw = 1; // Border width

            //spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, bw, r.Height), Color.White); // Left
            //spriteBatch.Draw(t, new Rectangle(r.Right, r.Top, bw, r.Height), Color.White); // Right
            //spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, r.Width, bw), Color.White); // Top
            //spriteBatch.Draw(t, new Rectangle(r.Left, r.Bottom, r.Width, bw), Color.White); // Bottom

        }

        private Point pStart = new Point(0, 0);
        private void DrawCoordinates()
        {
            MouseState ms = Mouse.GetState();

            if (InputManager.PreviousMouseState.LeftButton == ButtonState.Pressed &&
            InputManager.CurrentMouseState.LeftButton == ButtonState.Released)
            {
                if (pStart.X == 0)
                {
                    pStart = new Point(ms.X - RM.Get("Inventory").rect.X, ms.Y - RM.Get("Inventory").rect.Y);
                }
                else
                {
                    pStart = new Point(0, 0);
                }
            }

            int xCoord = InputManager.CurrentMouseState.X;// -RM.Get("Inventory").rect.X;
            int yCoord = InputManager.CurrentMouseState.Y;// -RM.Get("Inventory").rect.Y;

            string Message1 = string.Format("X:{0},  Y:{1}", xCoord, yCoord);
            string Message2 = string.Format("X:{0},  Y:{1}\n\nWIDTH:{2}\nHEIGHT: {3}", xCoord, yCoord, xCoord - pStart.X, pStart.Y - yCoord);

            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            if (pStart.X == 0)
            {
                spriteBatch.DrawString(Fonts.DebugFont, Message1, new Vector2((float)(ms.X + 40 + 1), (float)(ms.Y + 50 + 5 + 1)), Color.Black);
                spriteBatch.DrawString(Fonts.DebugFont, Message1, new Vector2((float)(ms.X + 40), (float)(ms.Y + 50 + 5)), Color.White);
            }
            else
            {
                spriteBatch.DrawString(Fonts.DebugFont, Message2, new Vector2((float)(ms.X + 40 + 1), (float)(ms.Y + 50 + 5 + 1)), Color.Black);
                spriteBatch.DrawString(Fonts.DebugFont, Message2, new Vector2((float)(ms.X + 40), (float)(ms.Y + 50 + 5)), Color.White);
            }
        }

        private void DrawLog(Vector2 position)
        {
            //position = new Vector2(5,328)
            SpriteBatch spriteBatch = screenManager.SpriteBatch;

            for (int i = 0; i <= 10; i++)
            {
                if (lm[10 - i] != null)
                {
                    spriteBatch.DrawString(Fonts.HeaderFont, lm[10 - i].Message, RM.Get("Inventory").GetVector() + position + new Vector2(0, i * 10), lm[10 - i].Clr);
                }
                
                spriteBatch.DrawString(Fonts.HeaderFont, lm[2].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 15), lm[2].Clr);
                spriteBatch.DrawString(Fonts.HeaderFont, lm[1].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 2 * 15), lm[1].Clr);
                spriteBatch.DrawString(Fonts.HeaderFont, lm[0].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 3 * 15), lm[0].Clr);
            }
            spriteBatch.DrawString(Fonts.HeaderFont, lm[3].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328), lm[3].Clr);
            spriteBatch.DrawString(Fonts.HeaderFont, lm[2].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 15), lm[2].Clr);
            spriteBatch.DrawString(Fonts.HeaderFont, lm[1].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 2 * 15), lm[1].Clr);
            spriteBatch.DrawString(Fonts.HeaderFont, lm[0].Message, RM.Get("Inventory").GetVector() + new Vector2(5, 328 + 3 * 15), lm[0].Clr);

         }



        private void DrawBuffs(Vector2 position, CreatureData c)
        {
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            List<string> ls = c.L.GetCumulatedBuffs();

            int counter = 1;
            foreach (string s in ls)
            {
                TM.GetAnimation(s).Draw(spriteBatch, position + RM.Get("Buff_" + counter).GetVector(), 0f);
                counter++;
            }

        }



        enum PlankState
        {
            Active,
            InActive,
            CantUse,
        }

        private void DrawCursor(GameTime gameTime)
        {
            SpriteBatch spriteBatch = screenManager.SpriteBatch;

            ItemData itemInHand = Root.I.GameSystem.ItemInHand;
            Texture2D cursorTexture = null;

            ////itemInHand = null;

            if (itemInHand == null)
            {
                if (an_cursor.Texture != TM.GetTex("Interface/Cursor"))
                {
                    an_cursor = TM.GetAnimation("Interface/Cursor");
                }
            }
            else
            {
                if (an_cursor.Texture != TM.GetTex(itemInHand.Gfx.Icon.Texture))
                {
                    an_cursor = TM.GetAnimation(itemInHand.Gfx.Icon.Texture);
                }
            }

            an_cursor.Draw(spriteBatch, new Vector2(InputManager.CurrentMouseState.X, InputManager.CurrentMouseState.Y), 0f);
        }
    }
}
